package com.wzcl.app.controller;

import com.wzcl.app.dao.model.AppUser;
import com.wzcl.app.dao.model.CoursePoint;
import com.wzcl.app.dao.model.VideoResources;
import com.wzcl.app.model.common.MyPage;
import com.wzcl.app.model.common.RVO;
import com.wzcl.app.model.common.WZCL;
import com.wzcl.app.model.dto.VideoResourceDto;
import com.wzcl.app.model.vo.VideoResourceVo;
import com.wzcl.app.service.CoursePointService;
import com.wzcl.app.service.UserCourseService;
import com.wzcl.app.service.UserService;
import com.wzcl.app.service.VideoResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@Log4j2
@RestController
@RequestMapping("/videoResource")
@Api(value = "课程视频素材controller", tags = {"课程视频素材管理接口"})
public class VideoResourceController{
    @Resource
    private VideoResourceService service;
    @Resource
    private CoursePointService coursePointService;
    @Resource
    private UserCourseService userCourseService;
    @Resource
    private UserService userService;

    @ApiOperation("创建课程的视频素材")
    @PostMapping("/add")
    public RVO<VideoResourceVo> addVideoResource(@RequestBody @Valid VideoResourceDto videoResourceDto){
        VideoResources videoResources = service.addVideoResource(videoResourceDto);
        return RVO.success(VideoResourceVo.create(videoResources));
    }

    @ApiOperation("创建课程的视频素材")
    @PostMapping("/del")
    public RVO<VideoResourceVo> delVideoResource(@RequestBody VideoResourceDto videoResourceDto){
        VideoResources videoResources = service.delVideoResourceById(videoResourceDto.getId());
        return RVO.success(VideoResourceVo.create(videoResources));
    }

    @ApiOperation("获得课程的视频素材List")
    @PostMapping("/getList")
    public RVO<List<VideoResourceVo>> getVideoResourceList(@RequestBody VideoResourceDto videoResourceDto){
        videoResourceDto.setUserPhone(SecurityContextHolder.getContext().getAuthentication().getName());
        List<VideoResources> list = service.getVideoResourceList(videoResourceDto);
        if(list != null && list.size()>0){
            Boolean flag = ownResource(videoResourceDto.getResourceId(),videoResourceDto.getResourceId());
            if(!flag)list.forEach(videoResources -> {
                if(WZCL.VIDEO_CHARGE_TYPE.CHARGE.equals(videoResources.getChargeType())){
                    videoResources.setFileKey(null);
                }
            });
            List<VideoResourceVo> collect = list.stream().map(VideoResourceVo::create).collect(Collectors.toList());
            return RVO.success(collect);
        }
        return RVO.success(null);
    }

    @ApiOperation("获得课程的视频素材page")
    @PostMapping("/getPage")
    public RVO<List<VideoResourceVo>> getVideoResourcePage(@RequestBody VideoResourceDto videoResourceDto){
        MyPage<VideoResources> page = service.getVideoResourcePage(videoResourceDto);
        if(page != null && page.getContent().size()>0){
            Boolean flag = ownResource(videoResourceDto.getResourceId(),videoResourceDto.getResourceId());
            if(!flag)page.getContent().forEach(videoResources -> {
                if(WZCL.VIDEO_CHARGE_TYPE.CHARGE.equals(videoResources.getChargeType())){
                    videoResources.setFileKey(null);
                }
            });
            List<VideoResourceVo> collect = page.getContent().stream().map(VideoResourceVo::create).collect(Collectors.toList());
            return RVO.success(collect);
        }
        return RVO.success(null);
    }

    public Boolean ownResource(Integer resourceId,Integer resourceType){
        try {
            String phone = SecurityContextHolder.getContext().getAuthentication().getName();
            AppUser user = userService.getAndCheckByPhone(phone);
            if(WZCL.USER_TYPE.ADMIN.equals(user.getType())|| WZCL.USER_TYPE.TEACHER.equals(user.getType())){
                return true;
            }
            Integer courseId;
            if(WZCL.VIDEO_RESOURCE_TYPE.COURSE_POINT.equals(resourceType)){
                CoursePoint point = coursePointService.getCoursePointById(resourceId);
                courseId = point.getCourseId();
            }else if(WZCL.VIDEO_RESOURCE_TYPE.COURSE.equals(resourceType)){
                courseId = resourceId;
            }else {
                return false;
            }
            return !userCourseService.findByPhoneAndCourseId(phone,courseId).isEmpty();
        }catch (Exception e){

        }
        return false;
    }

}
